package com.cypher.encryptionanddecryption.fragment.protocol.nb25;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.cypher.encryptionanddecryption.R;
import com.cypher.encryptionanddecryption.activity.BluetoothCommunicationActivity;
import com.cypher.encryptionanddecryption.bean.CommunicationBean;
import com.cypher.encryptionanddecryption.fragment.protocol.AbstractProtocolFragment;
import com.cypher.encryptionanddecryption.util.HexUtils;
import com.cypher.encryptionanddecryption.util.Nb25ProtocolUtils;

import org.apache.commons.lang3.StringUtils;

/**
 * 厂商自定义表状态页面
 * @author: 古霸天
 * @date: 2020/11/23
 */
public class VendorCustomMeterStatesFragment extends AbstractProtocolFragment {

    private final String[] onOrOffArr = {"未出厂", "已出厂"};
    private final String[] trueOrFalseArr = {"否", "是"};

    private TextView meterStateTxt;
    /**
     * 掉电状态
     */
    private Spinner customState1;
    /**
     * 欠压2状态
     */
    private Spinner customState2;
    /**
     * 微笑电量状态
     */
    private Spinner customState3;
    /**
     * 通讯失败
     */
    private Spinner customState4;
    /**
     * 温度传感器异常状态
     */
    private Spinner customState5;
    /**
     * 压力传感器异常状态
     */
    private Spinner customState6;
    /**
     * 生产状态使能位
     */
    private Spinner customState7;

    private CommunicationBean bean;
    private BluetoothCommunicationActivity activity;
    public VendorCustomMeterStatesFragment(BluetoothCommunicationActivity activity, CommunicationBean bean) {
        this.activity = activity;
        this.bean = bean;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_nb25_custom_meter_state, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init(view);
    }

    /**
     * 初始化
     * @param v
     */
    private void init(View v) {
        ArrayAdapter onOrOffAdapter = new ArrayAdapter<String>(v.getContext(), R.layout.spinner_item, onOrOffArr);
        onOrOffAdapter.setDropDownViewResource(R.layout.dropdown_stytle);

        ArrayAdapter trueOrFalseAdapter = new ArrayAdapter<String>(v.getContext(), R.layout.spinner_item, trueOrFalseArr);
        trueOrFalseAdapter.setDropDownViewResource(R.layout.dropdown_stytle);

        meterStateTxt = v.findViewById(R.id.meter_state_txt);

        customState1 = v.findViewById(R.id.spinner_custom_state1);
        customState1.setAdapter(trueOrFalseAdapter);
        customState2 = v.findViewById(R.id.spinner_custom_state2);
        customState2.setAdapter(trueOrFalseAdapter);
        customState3 = v.findViewById(R.id.spinner_custom_state3);
        customState3.setAdapter(trueOrFalseAdapter);
        customState4 = v.findViewById(R.id.spinner_custom_state4);
        customState4.setAdapter(trueOrFalseAdapter);
        customState5 = v.findViewById(R.id.spinner_custom_state5);
        customState5.setAdapter(trueOrFalseAdapter);
        customState6 = v.findViewById(R.id.spinner_custom_state6);
        customState6.setAdapter(trueOrFalseAdapter);

        customState7 = v.findViewById(R.id.spinner_custom_state7);
        customState7.setAdapter(onOrOffAdapter);

    }

    @Override
    public void fillFragment(String data) {
        reader(Nb25ProtocolUtils.formatReceiveData(data));
    }
    @Override
    public String getHexCode(String rwType) {
        if (StringUtils.equals("read", rwType)) {
            return Nb25ProtocolUtils.getReadCommand(bean.getHexCode(), PROTOCOL_VERSION);
        }
        // 组包
        return Nb25ProtocolUtils.getWriteCommand(bean.getHexCode(), getData(), PROTOCOL_VERSION);
    }

    /**
     * 渲染页面
     * @param data
     */
    private void reader(String data) {
        meterStateTxt.setText(data);

        String binary1 = HexUtils.paddingLeft(Integer.toBinaryString(Integer.parseInt(data.substring(0, 2), 16)), 8);
        int start1 = 1;
        customState1.setSelection(Integer.parseInt(getBinary(binary1, start1)));
        start1 += 1;
        customState2.setSelection(Integer.parseInt(getBinary(binary1, start1)));
        start1 += 2;
        customState3.setSelection(Integer.parseInt(getBinary(binary1, start1)));
        start1 += 1;
        customState4.setSelection(Integer.parseInt(getBinary(binary1, start1)));
        start1 += 1;
        customState5.setSelection(Integer.parseInt(getBinary(binary1, start1)));
        start1 += 1;
        customState6.setSelection(Integer.parseInt(getBinary(binary1, start1)));

        String binary2 = HexUtils.paddingLeft(Integer.toBinaryString(Integer.parseInt(data.substring(6, 8), 16)), 8);
        int start2 = 0;
        customState7.setSelection(Integer.parseInt(getBinary(binary2, start2)));
    }
    private String getBinary(String binary, int start) {
        return binary.substring(start, start + 1);
    }
    /**
     * 获取数据域
     * @return
     */
    private String getData() {
        StringBuffer state1 = new StringBuffer("0");
        state1.append(customState6.getSelectedItemPosition());
        state1.append(customState5.getSelectedItemPosition());
        state1.append("0");
        state1.append(customState4.getSelectedItemPosition());
        state1.append(customState3.getSelectedItemPosition());
        state1.append(customState2.getSelectedItemPosition());
        state1.append(customState1.getSelectedItemPosition());

        StringBuffer state2 = new StringBuffer();
        state2.append(customState7.getSelectedItemPosition());
        state2.append("0000000");

        String data1 = HexUtils.paddingLeft(Integer.toHexString(Integer.parseInt(state1.toString(), 2)), 2);
        String data2 = HexUtils.paddingLeft(Integer.toHexString(Integer.parseInt(state2.toString(), 2)), 2);
        return data1 + "0000" + data2;
    }
}
